package com.yipinhouse.employee.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.yipinhouse.common.exception.HouseException;
import com.yipinhouse.employee.common.HouseTypeResultCode;
import com.yipinhouse.employee.infra.model.WatchRecord;
import com.yipinhouse.employee.infra.mapper.WatchRecordMapper;
import com.yipinhouse.employee.service.WatchRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipinhouse.employee.service.param.InsertWatchRecordServiceParam;
import com.yipinhouse.employee.service.param.UpdateWatchRecordServiceParam;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 看房记录表 服务实现类
 * </p>
 *
 * @author WangCong
 * @since 2022-11-30
 */
@Service
public class WatchRecordServiceImpl extends ServiceImpl<WatchRecordMapper, WatchRecord> implements WatchRecordService {

    @Override
    public void insertWatchRecord(InsertWatchRecordServiceParam param) {
        String houseType = param.getHouseType();
        if (HouseTypeResultCode.NEW_HOUSING.getCode().equals(houseType)
                || HouseTypeResultCode.SECOND_HAND_HOUSING.getCode().equals(houseType)
                || HouseTypeResultCode.RENTAL_HOUSING.getCode().equals(houseType)) {
            WatchRecord watchRecord = BeanUtil.copyProperties(param, WatchRecord.class);
            watchRecord.setTime(new Date());
            save(watchRecord);
        }else throw new HouseException("房屋类型错误",5100);
    }

    @Override
    public void updateWatchRecordParam(UpdateWatchRecordServiceParam param) {
        if ((param.getOpinion()==null || param.getOpinion().trim().equals("") || param.getOpinion().equals("null"))
                && (param.getIntention() == null || param.getIntention().trim().equals("")|| param.getIntention().equals("null")))
            throw new HouseException("请输入修改内容",5101);
        WatchRecord watchRecord = getById(param.getId());
        if (watchRecord.getBrokerId() != param.getBrokerId()) throw new HouseException("只能修改自己的客户记录",5003);
        if (param.getIntention()!= null) watchRecord.setIntention(param.getIntention());
        if (param.getOpinion() != null) watchRecord.setOpinion(param.getOpinion());
        updateById(watchRecord);
    }
}
